package com.zqh.irmadscore.controller;


import cn.hutool.core.collection.CollUtil;
import com.zqh.irmadscore.model.dto.CombinationDTO;
import com.zqh.irmadscore.model.dto.TractCombinationDTO;
import com.zqh.irmadscore.model.entity.TractsCombination;
import com.zqh.irmadscore.service.CombinationService;
import com.zqh.irmadscore.utils.resultUtils.Result;
import com.zqh.irmadscore.utils.resultUtils.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequiredArgsConstructor
@Tag(name = "监测道组合")
@RequestMapping("/api")
public class CombinationController {


    private final CombinationService combinationService;

    @Operation(summary = "获取全部监测道组合id")
    @GetMapping
    public Result<List<TractsCombination>> getCombinationList()
    {
        List<TractsCombination> list = combinationService.list();

        return Result.ok(list);
    }

    @Operation(summary = "获取全部监测道组合")
    @GetMapping("/getCombination")
    public Result<List<TractCombinationDTO>> getTractCombinationDTO()
    {
        List<TractCombinationDTO> list = combinationService.getTractCombinationDTO();

        return Result.ok(list);
    }

    @Operation(summary = "获取全部监测道组合内容")
    @GetMapping("/getCombinationInfo")
    public Result<List<CombinationDTO>> getCombinationInfo()
    {
        List<CombinationDTO> list = combinationService.getCombinationInfo();
        return Result.ok(list);
    }

    @Operation(summary = "增加监测道组合")
    @PostMapping("addCombinationInfo")
    public Result<String> addCombinationInfo(@RequestBody TractsCombination tractsCombination)
    {
        combinationService.save(tractsCombination);
        return Result.ok();
    }

    @Operation(summary = "根据监测道查找组合")
    @GetMapping("/getCombinationInfoByTract")
    public Result<List<CombinationDTO>> getCombinationInfoByTract(
            @RequestParam(required = false) Integer tract1,
            @RequestParam(required = false) Integer tract2,
            @RequestParam(required = false) Integer tract3,
            @RequestParam(required = false) Integer tract4,
            @RequestParam(required = false) Integer tract5
    ){
        List<Integer> list = Arrays.asList(tract1,tract2,tract3,tract4,tract5);
        List<CombinationDTO> combinationDTOList = combinationService.getCombinationInfoByTract(list);

        if(CollUtil.isEmpty(combinationDTOList))
            return Result.fail(ResultCodeEnum.FAIL.getCode(),
                "未找到匹配的组合");


        return Result.ok(combinationDTOList);
    }




    @Operation(summary = "删除监测道组合")
    @DeleteMapping()
    public Result<String> deleteCombinationInfo(Integer id)
    {
        combinationService.removeById(id);
        return Result.ok("删除成功");
    }





}
